package com.jintoufs.web.action.image;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jintoufs.dao.QueryFilters;
import com.jintoufs.dao.QueryRule;
import com.jintoufs.domain.customer.Customer;
import com.jintoufs.domain.images.Images;
import com.jintoufs.domain.images.Placard;
import com.jintoufs.logAnnotation.SystemControllerLog;
import com.jintoufs.service.customer.CustomerService;
import com.jintoufs.service.hotel.HotelRoomService;
import com.jintoufs.service.image.ImageService;
import com.jintoufs.service.image.PlacardService;
import com.jintoufs.service.scene.SceneTicketService;
import com.jintoufs.service.tour.TourRouteService;
import com.jintoufs.web.action.shiro.bind.annotation.CurrentUser;
import com.jintoufs.web.action.support.JqgridPage;
import com.jintoufs.web.action.support.SupportAction;
import org.cosmos.modules.web.msg.AjaxMsg;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/placard")
public class PlacardAction extends SupportAction {

    @Resource
    private PlacardService placardService;

    @RequestMapping("/add")
    @ResponseBody
    public AjaxMsg addImage(@CurrentUser Customer customer, Placard placard) {
        AjaxMsg ajaxMsg = new AjaxMsg();
        placard.setCustomerId(customer.getId());
        placardService.insert(placard);
        ajaxMsg.setCode(AjaxMsg.SUCCESS);
        return ajaxMsg;
    }

    @RequestMapping("/query")
    @ResponseBody
    @SystemControllerLog(description = "获取酒店列表")
    public JqgridPage query(@CurrentUser Customer customer, HttpServletResponse response, int page, int rows, String sidx, String sord, String filters) {
        PageHelper.startPage(page, rows);
        QueryFilters filters_;
        if (filters == null || filters.trim().isEmpty()) {
            filters_ = new QueryFilters();
        } else {
            filters_ = JSON.parseObject(filters, QueryFilters.class);
        }
//        QueryRule queryRule = new QueryRule();
//        queryRule.setField("customer_id");
//        queryRule.setData(customer.getId());
//        filters_.getRules().add(queryRule);
        filters_.setSidx(sidx);
        filters_.setSord(sord);
        List<Map<String, Object>> list = placardService.getByFilter(filters_);
        // 用PageInfo对结果进行包装
        PageInfo pageInfo = new PageInfo(list);
        return new JqgridPage(pageInfo.getTotal(), pageInfo.getPages(), pageInfo.getPageNum(),
                pageInfo.getList());
    }

    @RequestMapping("/input")
    public String input(@CurrentUser Customer customer, Placard placard) {
        return "images/placard-input";
    }

    @RequestMapping("/list")
    public String list(@CurrentUser Customer customer, Model model) {
        HashMap map = new HashMap();
        map.put("customerId", customer.getId());
        List<Placard> placards = placardService.selectByparams(map);
        model.addAttribute("placards", placards);
        return "images/placard-list";
    }

    @RequestMapping("delete")
    public String delete(Integer id) {
        placardService.delete(id);
        return "redirect:../images/placard-list";
    }
}
